#ifndef __RIGELPRINT_H__
#define __RIGELPRINT_H__

#include <stdint.h>
#include <inttypes.h>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cassert>

class RigelPrint {
  public:
    struct print {
      int core;
      int thread;
      uint64_t cycle;
      uint32_t value;
    };

    RigelPrint(const char *filename) {
      FILE *f = fopen(filename, "r");
      assert(f != NULL);
      print p;
      int dummy;
      float dummyf;
      int ret;
      char dummys[256];
      while((ret = fscanf(f, "0x%"SCNx64" %s %s %s 0x%X %s %s core: %d thread: %d\n",
             &(p.cycle), dummys, dummys, dummys, &(p.value), dummys, dummys, &(p.core), &(p.thread))) == 9) {
        prints.push_back(p);
      }
      fclose(f);
    }

    double value_mean() const {
      uint64_t total = 0;
      for(std::vector<print>::const_iterator it = prints.begin(), end = prints.end(); it != end; ++it) {
        total += it->value;
      }
      return ((double)total/prints.size());
    }

    double value_sd() const {
      double mean = value_mean();
      double total = 0;
      for(std::vector<print>::const_iterator it = prints.begin(), end = prints.end(); it != end; ++it) {
        total += (it->value - mean)*(it->value - mean);
      }
      return sqrt(total/prints.size());
    }
    
  private:
    std::vector<print> prints;
};

#endif //#ifndef __RIGELPRINT_H__
